'''
Created on May 7, 2011

@author: yc30v1m
'''
from library.testresults.AbstractReporter import AbstractReporter
from library.AutoLoad import AutoLoad

from logging import getLogger
log = getLogger(__name__)

class Testlink(AbstractReporter):
    '''
    classdocs
    '''
    __testLinkPlugin = None
   

    def __init__(self, setupInfo):
        super(self.__class__, self).__init__()
        log.debug("TestLink Test Result reported Inatilized")
        self.__createClient()
        
    def updateResult(self, instance):
        log.debug("Updating results in Testlink for TCID: %s" + instance.tcid)
        try:
            message = self.__testLinkPlugin.reportTCResult(instance.tcid,instance.tcResult,instance.tcNotes)
            #print message[0]['message']
            #if message[0]['message'] != 'Success!':
            #    log.warning("Success was not recieved from TestLink" + str(message))
        except:
            log.critical("Exception Occured TCID: %s: TestResult: %s" % (instance.tcid, instance.tcResult))
            AutoLoad.logExceptionInfo()
         
        
    def __createClient(self):
        try:
            apikey = self.configurationFileObj.getOptionFromSection('testcases_testlink', 'apikey')
            url = self.configurationFileObj.getOptionFromSection('testcases_testlink', 'url')
            testProject = self.configurationFileObj.getOptionFromSection('testcases_testlink', 'project_name')
            testProjectPrefix = self.configurationFileObj.getOptionFromSection('testcases_testlink', 'project_prefix')
            testPlan = self.configurationFileObj.getOptionFromSection('testcases_testlink', 'testplan')
            TlClient = AutoLoad.ClassOnly('plugins.testlink.Client')
            self.__testLinkPlugin = TlClient(apikey,testProject,testPlan, url, testProjectPrefix)
        except:
            log.critical("plugin setup problem occured")
            AutoLoad.logExceptionInfo()

        
